//
//  SearchForDiseaseAddPicsForIPadCell.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/9.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "SearchForDiseaseAddPicsForIPadCell.h"

#define PhotoCellID @"cellid1"
@implementation SearchForDiseaseAddPicsForIPadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}
- (NSMutableArray *)picsArray{
    if (_picsArray == nil) {
        _picsArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _picsArray;
}
- (NSMutableArray *)deleteArray{
    if (_deleteArray ==nil) {
        _deleteArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _deleteArray;
}
- (void)setUI{
    self.titleLab = [[UILabel alloc]init];
    self.titleLab.textColor = Black_Color;
    self.titleLab.font = [UIFont systemFontOfSize:13*PadWidthRate];
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(30*PadHeightRate);
        make.left.mas_equalTo(35*PadWidthRate);
        make.right.mas_equalTo(-35*PadWidthRate);
    }];
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    [fl setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    fl.minimumInteritemSpacing = 0;//垂直间距
    fl.minimumLineSpacing= 14*PadWidthRate;//水平间距
    fl.itemSize = CGSizeMake(90*PadHeightRate, 90*PadHeightRate);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
    fl.sectionInset = UIEdgeInsetsMake(10*PadHeightRate, 10*PadWidthRate, 10*PadHeightRate, 10*PadWidthRate);
    self.collection.backgroundColor = Bai_Color;
    [self.collection registerClass:[PhotoColCell class] forCellWithReuseIdentifier:PhotoCellID];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.showsVerticalScrollIndicator = NO;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.layer.borderColor = [Line_Color CGColor];
    self.collection.layer.borderWidth = 1;
    self.collection.layer.cornerRadius = 5;
    self.collection.layer.masksToBounds = YES;
    [self.contentView addSubview:self.collection];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30*PadHeightRate);
        make.bottom.mas_equalTo(-6*PadHeightRate);
        make.left.mas_equalTo(23*PadWidthRate);
        make.right.mas_equalTo(-23*PadWidthRate);
    }];
}
- (void)addValue:(NSArray *)arr{
    [self.picsArray removeAllObjects];
    [self.picsArray addObjectsFromArray:arr];
    [self.collection reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.isReset) {
        return self.picsArray.count+1;
    }else{
        return self.picsArray.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCellID forIndexPath:indexPath];
    cell.indexPath = indexPath;
    
    if (indexPath.row == self.picsArray.count) {
        cell.img.image = [UIImage imageNamed:@"add_picture"];
    
    }else{
        //    执行上传图片过程
        
        if ([self.picsArray[indexPath.row] isKindOfClass:[UIImage class]]) {
            cell.img.image = self.picsArray[indexPath.row];
        }else{
            [cell.img sd_setImageWithURL:[NSURL URLWithString:self.picsArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"1"]];
        }
       
      
        //    执行上传图片过程
    
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.picsArray.count) {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = NO;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self.con presentViewController:imagePicker animated:YES completion:nil];
        }]];
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = NO;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self.con presentViewController:imagePicker animated:YES completion:nil];
        }]];
        
        actionSheet.popoverPresentationController.sourceView = self.collection;
        actionSheet.popoverPresentationController.sourceRect = CGRectMake(9*PadWidthRate+44*PadHeightRate+(indexPath.row*(90*PadHeightRate+14*PadWidthRate)),(CGFloat)self.collection.frame.size.height/(CGFloat)2.0, 1.0, 1.0);
        [self.con presentViewController:actionSheet animated:YES completion:nil];
       
    }else{
        BigPicViewController *con = [[BigPicViewController alloc]init];
        con.imgArray = self.picsArray;
        con.delegate = self;
        con.allowDelete = self.isReset;
        [con.deleteArray removeAllObjects];
        [con.deleteArray addObjectsFromArray:self.deleteArray];
        con.imgIndex = indexPath.row;
        [self.con presentViewController:con animated:YES completion:nil];
    }
    
}

#pragma mark UIImagePickerController Delegate
- (void)bigPicDelegateWithImgArray:(NSMutableArray *)imgArray deleteArray:(NSMutableArray *)deleteArray{
    [self.picsArray removeAllObjects];
    [self.picsArray addObjectsFromArray:imgArray];
    [self.deleteArray removeAllObjects];
    [self.deleteArray addObjectsFromArray:deleteArray];
    [self.collection reloadData];
    if ([self.delegate respondsToSelector:@selector(picsForIPadWithImgArray:AndDeleteArray:)]) {
        [self.delegate picsForIPadWithImgArray:self.picsArray AndDeleteArray:self.deleteArray];
    }
}
//相机
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    
    
    [self.picsArray addObject:image];
    if ([self.delegate respondsToSelector:@selector(picsForIPadWithImgArray:AndDeleteArray:)]) {
        [self.delegate picsForIPadWithImgArray:self.picsArray AndDeleteArray:self.deleteArray];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self.collection reloadData];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    return;
}

@end
